
import os
from appium import webdriver
from selenium.webdriver.support import expected_conditions
from appium.webdriver.common.touch_action import TouchAction

class Base:
    def __init__(self,driver,wait):
        self.driver = driver
        self.wait = wait

        # 获取当前文件目录和上层目录绝对路径
        cur_dir = os.path.dirname(os.path.abspath(__file__))
        parent_dir = os.path.dirname(cur_dir)

        # 设置log文件夹位置，存放截图
        self.log_dir = os.path.join(parent_dir, 'log')

#点击个人页面操作
    def click_personal_homepage(self,locator):
      try:
          self.wait.until(expected_conditions.presence_of_element_located(locator))
          self.driver.find_element(*locator).click()
      except Exception as e:
          print(e)
          self.driver.save_screenshot(os.path.join(self.log_dir, '点击个人页面错误.png'))
          raise Exception("点击个人页面错误")

#点击订阅管理操作
    def click_subscription_management(self,locator):
      try:
          self.wait.until(expected_conditions.presence_of_element_located(locator))
          self.driver.find_element(*locator).click()
      except Exception as e:
          print(e)
          self.driver.save_screenshot(os.path.join(self.log_dir, '点击订阅管理错误.png'))
          raise Exception("点击订阅管理错误")

#点击登出按钮
    def click_logout_button(self,locator):
      try:
          self.wait.until(expected_conditions.presence_of_element_located(locator))
          self.driver.find_element(*locator).click()
      except Exception as e:
          print(e)
          self.driver.save_screenshot(os.path.join(self.log_dir,'点击登出错误.png'))
          raise Exception("点击登出错误")

# 点击确认登出按钮
    def click_confirm_logout(self,locator):
       try:
          self.wait.until(expected_conditions.presence_of_element_located(locator))
          self.driver.find_element(*locator).click()
       except Exception as e:
          print(e)
          self.driver.save_screenshot(os.path.join(self.log_dir, '点击确定登出错误.png'))
          raise Exception("点击确定登出错误")

# 输入账号操作
    def input_user_account_number(self,locator,user):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
            self.driver.find_element(*locator).clear()
            self.driver.find_element(*locator).send_keys(user)
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '输入账号错误.png'))
            raise Exception("输入账号错误")

# 输入密码操作
    def input_user_pwd(self,locator,pwd):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
            self.driver.find_element(*locator).clear()
            self.driver.find_element(*locator).send_keys(pwd)
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '输入密码错误.png'))
            raise Exception("输入密码错误")

# 点击隐私政策操作
    def click_privacy_policy(self,locator):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '点击隐私政策错误.png'))
            raise Exception("点击隐私政策错误")


#点击谷歌按钮操作
    def click_to_google(self,locator):
      try:
          self.wait.until(expected_conditions.presence_of_element_located(locator))
          self.driver.find_element(*locator).click()
      except Exception as e:
          print(e)
          self.driver.save_screenshot(os.path.join(self.log_dir, '点击谷歌按钮错误.png'))
          raise Exception("点击谷歌按钮错误")

#确认确认谷歌按钮操作
    def click_to_confirm_google(self, locator):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '点击谷歌确认按钮错误.png'))
            raise Exception("点击谷歌确认按钮错误")

#点击登录操作
    def click_login(self,locator):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '点击登录错误.png'))
            raise Exception("点击登录错误")

#点击立即注册操作
    def click_register_now_button(self,locator):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '点击立即注册失败.png'))
            raise Exception("点击立即注册失败")

#输入邮箱操作
    def email_input(self,locator,email):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
            self.driver.find_element(*locator).clear()
            self.driver.find_element(*locator).send_keys(email)
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '输入邮箱错误.png'))
            raise Exception("输入邮箱错误")

#输入密码操作
    def enter_password(self,locator,password):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
            self.driver.find_element(*locator).clear()
            self.driver.find_element(*locator).send_keys(password)
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '输入密码有误.png'))
            raise Exception("输入密码有误")

#确认密码操作
    def re_enter_password(self, locator, re_password):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
            self.driver.find_element(*locator).clear()
            self.driver.find_element(*locator).send_keys(re_password)
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '再次输入密码有误.png'))
            raise Exception("再次输入密码有误")

#点击注册操作
    def click_register(self,locator):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '点击注册错误.png'))
            raise Exception("点击注册错误")
#点击OK按钮操作
    def click_ok_button(self,locator):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(locator))
            self.driver.find_element(*locator).click()
        except Exception as e:
            print(e)
            self.driver.save_screenshot(os.path.join(self.log_dir, '点击ok按钮错误.png'))
            raise Exception("点击OK按钮错误")


